/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.generate.service.impl.GenerateServiceImpl
   Module Description   :

   Date Created      : 2009/12/29
   Original Author   : feeling.wang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.generate.remote;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.generate.service.GenerateService;
import com.yaodian100.ec.search.entity.Keyword;
import com.yaodian100.ec.search.service.KeywordService;
import com.yaodian100.ec.supplierapply.remote.RemoteSupplierApplyServiceImpl;

/**
 * @author feeling.wang
 * 
 */
public class RemoteGenerateServiceImpl implements RemoteGenerateService {
	private static Logger logger = LoggerFactory.getLogger(RemoteSupplierApplyServiceImpl.class);
	@Autowired
	private GenerateService generateService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private MenuService menuService;
	@Autowired
	private KeywordService keywordService;
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public boolean generateCategoryIndexV2(String date) throws CoreException {
		logger.info("start, date:{}", date);
		generateService.generateCategoryIndexV2(date);
		logger.info("end");
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.generate.remote.RemoteGenerateService#generateCateMainGoldAdv(java.lang.Long)
	 */
	@Override
	public boolean generateCateMainGoldAdv(String date) throws CoreException, IOException {
		logger.info("start, date:{}", date);
		generateService.generateCateMainGoldAdv(date);
		logger.info("end");
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.generate.remote.RemoteGenerateService#generateCuratorRecommV2(java.lang.Long)
	 */
	@Override
	public boolean generateCuratorRecommV2(String categoryId) throws CoreException, IOException {
		logger.info("start, category:{}", categoryId);
		generateService.generateCuratorRecommV2(Long.parseLong(categoryId));
		logger.info("end");
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.generate.remote.RemoteGenerateService#generateCuratorRecommV3(java.lang.String)
	 */
	@Override
	public boolean generateCuratorRecommV3() throws CoreException, IOException {
		logger.info("start");
		File file = new File(ServerValue.getFrontContentHtmlPath() + "PRODUCTRC");
		if (file.exists()) {
			FileUtils.forceDelete(file);
		}

		String sql = "select distinct(category_id) from EC_PROD_CAT_RC";
		List categoryIdList = jdbcTemplate.queryForList(sql);
		for (int i = 0; i < categoryIdList.size(); i++) {
			Map categoryIdMap = (Map) categoryIdList.get(i);
			String categoryId = ((BigDecimal) categoryIdMap.get("CATEGORY_ID")).toString();
			generateService.generateCuratorRecommV3(categoryId);
		}

		logger.info("end");
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.generate.remote.RemoteGenerateService#generateHotSelectV2(java.lang.Long)
	 */
	@Override
	public boolean generateHotSelectV2(String categoryId) throws CoreException, IOException {
		logger.info("start, category:{}", categoryId);
		generateService.generateHotSelectV2(Long.parseLong(categoryId));
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateIdxBlockAdv_V2(String date) throws CoreException {
		logger.info("start, date:{}", date);
		generateService.generateIdxMainGoldAdv_V2(date);
		generateService.generatePclMainGoldAdv_V2(date);
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateIdxGoldFourV2(String date) throws CoreException, Exception {
		logger.info("start, date:{}", date);
		generateService.generateIdxGoldFourV2(date);
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateIdxHotSaleV2(String date) throws CoreException, Exception {
		logger.info("start, date:{}", date);
		generateService.generateIdxHotSaleV2(date);
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateIndexNews(String date) throws CoreException, Exception {
		logger.info("start, date:{}", date);
		// 建立首頁 最新快报
		generateService.generateIdxNews(date);
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateNaviBar(String date) throws CoreException {
		logger.info("start, date:{}", date);
		generateService.generateNaviBar(date);
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateRightDownAdvV2(String categoryId) throws CoreException, Exception {
		logger.info("start, category:{}", categoryId);
		generateService.generateRightDownAdvV2(Long.parseLong("0"));
		logger.info("end");
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.generate.remote.RemoteGenerateService#generateSearchbar_V2(java.lang.String)
	 */
	@Override
	public boolean generateSearchbar_V2(String categoryId) throws CoreException {
		logger.info("start, categoryId:{}", categoryId);
		if (StringUtils.isNotBlank(categoryId)) {
			generateService.generateSearchbar_V2(categoryId);
		} else {
			List<Keyword> keys = keywordService.getAll(0, -1, null);
			for (Keyword k : keys) {
				generateService.generateSearchbar_V2(k.getCategoryId().toString());
			}
		}
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateSeoService() throws CoreException {
		try {
			File seofile = new File(ServerValue.getFrontApachePath() + "/static/server/frontpage.properties");
			FileUtils.forceDeleteOnExit(seofile);
			seofile = new File(ServerValue.getFrontApachePath() + "/static/server/frontpage.properties");

			File file = new File(ServerValue.getFrontApachePath() + "/static/server/seo_basic.txt");
			String content = FileUtils.readFileToString(file, "UTF-8");
			Reader reader = new StringReader(content);
			Properties props = new Properties();
			props.load(reader);
			reader.close();
			Set<Object> key = props.keySet();
			Iterator<Object> iter = key.iterator();
			while (iter.hasNext()) {
				Object obj = iter.next();
				props.put(obj, props.get(obj));
			}
			FileOutputStream writer = new FileOutputStream(seofile);
			props.store(writer, "");
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	@Override
	public boolean generateSideBarMenu(String date) throws CoreException, IOException {
		logger.info("start, date:{}", date);
		generateService.generateSideBarMenu2New(date);
		logger.info("end");
		return true;

	}

	@Override
	public boolean generateSpecialAct(String categoryId) throws CoreException, IOException {
		logger.info("start, category:{}", categoryId);
		generateService.generateSpecialAct(Long.parseLong(categoryId));
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateXMLforBaidu(String date) throws CoreException {
		logger.info("start, date:{}", date);
		generateService.generateXMLforBaidu();
		// 返利的順便一起產生
		generateService.generateXMLforFanli();
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateDaphneCategoryIndex(String date) {
		return generateService.generateDaphneCategoryIndex(date);
	}

	@Override
	public boolean generateCategoryTopRank(String categoryIdString) throws CoreException, IOException {
		logger.info("start, category:{}", categoryIdString);
		generateService.generateCategoryTopRank(categoryIdString);
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateBrandIndex(String date) throws CoreException, IOException {
		return generateService.generateBrandIndex(date);
	}

	// 首頁通欄廣告
	@Override
	public boolean generateIdxTopSpanAdv(String s) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generateIdxTopSpanAdv();
		this.generateService.generateIdxImageAdv("PCL_TOPSPAN", "IDX_TOPSPAN");
		logger.info("end");
		return true;
	}

	// 首頁腰線廣告
	@Override
	public boolean generateIdxWaistline(String s) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generateIdxWaistline();
		logger.info("end");
		return true;
	}

	// 首頁右側小廣告1
	@Override
	public boolean generateIdxRightSmall1(String s) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generateIdxRightSmall1();
		logger.info("end");
		return true;
	}

	// 首頁右側小廣告2
	@Override
	public boolean generateIdxRightSmall2(String s) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generateIdxRightSmall2();
		logger.info("end");
		return true;
	}

	// 首頁雙拼廣告左
	@Override
	public boolean generateIdxTwinLeft(String s) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generateIdxTwinLeft();
		logger.info("end");
		return true;
	}

	// 首頁雙拼廣告右
	@Override
	public boolean generateIdxTwinRight(String s) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generateIdxTwinRight();
		logger.info("end");
		return true;
	}

	// 首頁本週推薦
	@Override
	public boolean generateIdxWeeklyRecommendAdv(String s) throws Exception {
		logger.info("start");
		this.generateService.generateIdxWeeklyRecommendAdv();
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateIdxSaleLayoutAdv_V3(String date) throws CoreException {
		logger.info("start, date:{}", date);
		generateService.generateIdxMidBoxAdv_V3(date);
		logger.info("end");
		return true;
	}

	// 首頁热销横幅
	@Override
	public boolean generateIdxHotSaleBanner(String s) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generateIdxImageAdv("IDX_HOTSALE_BANNER");
		this.generateService.generateIdxImageAdv("PCL_HOTSALE_BANNER", "IDX_HOTSALE_BANNER");
		logger.info("end");
		return true;
	}

	// 首頁腰线广告V5
	@Override
	public boolean generateIdxWaistline_V5(String s) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generateIdxImageAdv("IDX_WAISTLINE_V5");
		this.generateService.generateIdxImageAdv("PCL_WAISTLINE_V5", "IDX_WAISTLINE_V5");
		logger.info("end");
		return true;
	}

	// 首页银行分期广告
	@Override
	public boolean generateIdxBankBanner(String s) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generateIdxImageAdv("IDX_BANK_BANNER");
		this.generateService.generateIdxImageAdv("PCL_BANK_BANNER", "IDX_BANK_BANNER");
		logger.info("end");
		return true;
	}

	// 四头标
	@Override
	public boolean generateTop4Tag(String advType) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generateTop4Tag();
		logger.info("end");
		return true;
	}

	@Override
	public boolean generaterIdxHotSaleV5(String date) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generaterIdxHotSaleV5();
		this.generateService.generaterPclHotSaleV5();
		logger.info("end");
		return true;
	}

	@Override
	public boolean generaterIdxSpecialAct(String date) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generaterIdxSpecialAct();
		this.generateService.generaterPclSpecialAct();
		logger.info("end");
		return true;
	}

	@Override
	public boolean generaterIdxSaleV5(String date) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generaterIdxSaleV5();
		this.generateService.generaterPclSaleV5();
		logger.info("end");
		return true;
	}

	@Override
	public boolean generaterIdxBrandRecommend(String date) throws CoreException, Exception {
		logger.info("start");
		this.generateService.generaterIdxBrandRecommend();
		this.generateService.generaterPclBrandRecommend();
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateOutletsBannerAdv() throws CoreException, Exception {
		logger.info("start");
		this.generateService.generateOutletsBannerAdv();
		logger.info("end");
		return true;
	}

	// 嵌入資料到活動頁面
	@Override
	public boolean generateEventHtml(Map map, String fileName, String templateName) throws CoreException {
		logger.info("start");
		if (StringUtils.isNotBlank(fileName) && StringUtils.isNotBlank(templateName)) {
			this.generateService.generateEventHtml(map, fileName, templateName);
		} else {
			logger.info("值為空,fileName=" + fileName + ",templateName=" + templateName);
		}
		logger.info("end");
		return true;
	}

	@Override
	public boolean generateEventHtmlFormeunKey(String menuKey, String fileName, String templateName) throws CoreException {
		if (StringUtils.isNotBlank(menuKey) && StringUtils.isNotBlank(fileName) && StringUtils.isNotBlank(templateName)) {
			Menu menu = menuService.get(menuKey);
			if (menu != null) {
				generateEventHtml(menu.getOptions(), fileName, templateName);
			} else {
				logger.info("找不到menu,menuKey=" + menuKey);
			}
		} else {
			logger.info("值為空,menuKey=" + menuKey + ",fileName=" + fileName + ",templateName=" + templateName);
		}
		return true;
	}
}